package com.hanhanxiaochong.zhongxiangchuanmei.presenter;

import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopListBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopTypeBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.SymptomBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IGoodListView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/5/24.
 */

public class GoodListPresenter extends BasePresenter<IGoodListView> {

    public GoodListPresenter(IGoodListView iView) {
        super(iView);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {

    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }

    public void getShopType(String id) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).getShopType(id, "3")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<ShopTypeBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<ShopTypeBean> response) {
                        iView.setShopType(response.getData().getData_list());
                    }
                }));
    }

    public void getSymptoms() {
        addSubscription(RetrofitSource.createApi(HttpPostService.class).symptom()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<SymptomBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<SymptomBean>> response) {
                        iView.setSymptoms(response.getData());
                    }
                }));
    }

    /**
     * 商城列表
     *
     * @param name     商品名称[默认为空]
     * @param t_id     二级分类id
     * @param comment  筛选评价0:升序；1：降序
     * @param price    筛选价格0:升序；1：降序
     * @param sell_num 筛选销量0:升序；1：降序
     * @param self_sup 筛选是否自营(y:查看自营；n：看合盈)
     * @param price_s  筛选价格区间最低价
     * @param price_d  筛选价格区间最高价
     * @param s_id     筛选三级分类id多选以,隔开
     * @param symptom  筛选功效id多选以,隔开
     * @param page     页数
     */
    public void getList(String name, String t_id, String comment, String price, String sell_num,
                        String self_sup, String price_s, String price_d, String s_id, String symptom, String page) {
        Map<String, String> map = new HashMap<>();
        map.put("name", name);
        map.put("t_id", t_id);
        map.put("comment", comment);
        map.put("price", price);
        map.put("sell_num", sell_num);
        map.put("self_sup", self_sup);
        map.put("price_s", price_s);
        map.put("price_d", price_d);
        map.put("s_id", s_id);
        map.put("symptom", symptom);
        map.put("page", page);
        map.put("size", "20");
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .shop_list(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<ShopListBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<ShopListBean>> response) {
                        if (page.equals("1")) {
                            iView.setData(response.getData());
                        } else {
                            iView.addData(response.getData());
                        }
                    }
                }));
    }
}
